/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.outHouse.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.outHouse.domain.OrderDetail;
import me.zhengjie.suli.outHouse.repository.OrderDetailRepository;
import me.zhengjie.suli.outHouse.service.OrderDetailService;
import me.zhengjie.suli.outHouse.service.dto.OrderDetailDto;
import me.zhengjie.suli.outHouse.service.dto.OrderDetailQueryCriteria;
import me.zhengjie.suli.outHouse.service.mapstruct.OrderDetailMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wyh
* @date 2024-02-01
**/
@Service
@RequiredArgsConstructor
public class OrderDetailServiceImpl implements OrderDetailService {

    private final OrderDetailRepository orderDetailRepository;
    private final OrderDetailMapper orderDetailMapper;

    @Override
    public Map<String,Object> queryAll(OrderDetailQueryCriteria criteria, Pageable pageable){
        Page<OrderDetail> page = orderDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(orderDetailMapper::toDto));
    }

    @Override
    public List<OrderDetailDto> queryAll(OrderDetailQueryCriteria criteria){
        return orderDetailMapper.toDto(orderDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OrderDetailDto findById(String id) {
        OrderDetail orderDetail = orderDetailRepository.findById(id).orElseGet(OrderDetail::new);
        ValidationUtil.isNull(orderDetail.getId(),"OrderDetail","id",id);
        return orderDetailMapper.toDto(orderDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderDetailDto create(OrderDetail resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return orderDetailMapper.toDto(orderDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OrderDetail resources) {
        OrderDetail orderDetail = orderDetailRepository.findById(resources.getId()).orElseGet(OrderDetail::new);
        ValidationUtil.isNull( orderDetail.getId(),"OrderDetail","id",resources.getId());
        orderDetail.copy(resources);
        orderDetailRepository.save(orderDetail);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            orderDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OrderDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrderDetailDto orderDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("出库明细单号", orderDetail.getOrderDetailCode());
            map.put("主表ID", orderDetail.getMainId());
            map.put("物料编码", orderDetail.getMaterialCode());
            map.put("物料名称", orderDetail.getMaterialName());
            map.put("规格", orderDetail.getSpecification());
            map.put("批次号", orderDetail.getLotCode());
            map.put("载具", orderDetail.getTrayCode());
            map.put("计划数量", orderDetail.getPlanQty());
            map.put("实际数量", orderDetail.getActQty());
            map.put("单位", orderDetail.getUnitCode());
            map.put("库位号", orderDetail.getLocationCode());
            map.put("仓库号", orderDetail.getWarehouseCode());
            map.put("序列号", orderDetail.getSerialNumber());
            map.put("创建用户", orderDetail.getCreateBy());
            map.put("修改用户", orderDetail.getUpdateBy());
            map.put("创建日期", orderDetail.getCreateTime());
            map.put("修改日期", orderDetail.getUpdateTime());
            map.put("仓位", orderDetail.getZone());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public ResponseEntity<Object> findDetailByMainId(String id) {
        return new ResponseEntity<>(orderDetailRepository.findByMainId(id), HttpStatus.OK);
    }
}